<?php

/**
 * uniBoard ACP Forums List
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_forums{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Cache output
		ob_start();
		
		// Parents stats
		$parent_stat = array();
		
		// Forums
		$forums_list = array();
		
		// Select Forums
		$appCore -> db -> query( 'SELECT forum_id, forum_parent, forum_pos, forum_type, forum_name, forum_threads, forum_posts
		FROM ' . DB_PREFIX . 'forums
		ORDER BY forum_pos');
		
		// Pass Them to array
		while ( $forum_result = $appCore -> db -> fetch_assoc())
		{
			// Count stat
			if ( isset( $parent_stat[$forum_result['forum_parent']]))
			{
				$parent_stat[$forum_result['forum_parent']] ++;
			}
			else
			{
				$parent_stat[$forum_result['forum_parent']] = 1;
			}
			
			// Add to list
			$forums_list[$forum_result['forum_parent']][$forum_result['forum_id']] = $forum_result;
		}
	
		// Free Result
		$appCore -> db -> freeResult();
		
		// Draw Forums?
		if ( empty($forums_list))
		{
			$appCore -> output -> error( $appCore -> lang['acp_forums_not_found'], $appCore -> lang['acp_forums']);	
		}
		else
		{
			// Reorder?
			if ( $appCore -> _POST('reorder_forums') == 1)
			{
				// Grab forums
				$forums_positions = $appCore -> _POST('forum_pos');
				
				// Reiterate
				foreach ( $forums_positions as $forum_id => $forum_pos)
				{
					// Post allright?
					if ( !is_array( $forum_pos))
					{
						// Force types
						settype( $forum_id, 'integer');
						settype( $forum_pos, 'integer');
						
						// Update?
						if ( $forum_id > 0)
						{
							$appCore -> db -> update( array('forum_pos' => $forum_pos), 'forums', 'forum_id = \'' . $forum_id . '\'');
						}
					}
				}
				
				// Flush cache
				class_cache::flushCache( 'forums');
				class_cache::flushCache( 'forums_list');
				class_cache::flushCache( 'forums_loop');
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_reoreded_forums');
				
				// Redirect us back to list
				$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=forums'), $appCore -> lang['acp_forums_reordered']);
			}
			else
			{
				// Order forums
				$forums_list = class_hierarchy::doOrdering( $forums_list, array(
					$appCore -> output -> drawImage( 'tree_s'),
					$appCore -> output -> drawImage( 'tree_i'),
					$appCore -> output -> drawImage( 'tree_t'),
					$appCore -> output -> drawImage( 'tree_l')
				));
				
				// In category?
				$in_cat = false;
				
				// Reorder forums
				echo $appCore -> output -> openForm( $appCore -> adminLink('act=forums'));
				echo $appCore -> drawHidden( 'reorder_forums', 1);
				
				// Begin Drawing?
				foreach ( $forums_list as $forum_id => $forum_data)
				{
					// Make forum actions
					$forum_actions = '<a href="' . $appCore -> adminLink( 'act=edit_forum&do=edit&forum=' . $forum_id) . '">' . $appCore -> output -> drawImage( 'action_edit', $appCore -> lang['acp_forums_op_delete_content']) . ' ' . $appCore -> lang['acp_forums_op_edit'] . '</a>
					<div><b>' . $appCore -> lang['acp_forums_op_sect_tools'] . '</b></div>
					<a href="' . $appCore -> adminLink( 'act=edit_forum&do=perms&forum=' . $forum_id) . '">' . $appCore -> output -> drawImage( 'action_permissions', $appCore -> lang['acp_forums_op_perms']) . ' ' . $appCore -> lang['acp_forums_op_perms'] . '</a>
					<a href="' . $appCore -> adminLink( 'act=edit_forum&do=resynch&forum=' . $forum_id) . '">' . $appCore -> output -> drawImage( 'action_refresh', $appCore -> lang['acp_forums_op_resynch']) . ' ' . $appCore -> lang['acp_forums_op_resynch'] . '</a>
					<div><b>' . $appCore -> lang['acp_forums_op_sect_delete'] . '</b></div>
					<a href="' . $appCore -> adminLink( 'act=edit_forum&do=prune&forum=' . $forum_id) . '">' . $appCore -> output -> drawImage( 'action_delete', $appCore -> lang['acp_forums_op_delete_content']) . ' ' . $appCore -> lang['acp_forums_op_delete_content'] . '</a>
					<a href="' . $appCore -> adminLink( 'act=edit_forum&do=delete&forum=' . $forum_id) . '">' . $appCore -> output -> drawImage( 'action_delete', $appCore -> lang['acp_forums_op_delete_forum']) . ' ' . $appCore -> lang['acp_forums_op_delete_forum'] . '</a>';
					
					// Forum Parent?
					if ( $forum_data['forum_parent'] == 0)
					{
						// Close previous block?
						if ( $in_cat)
						{
							echo $appCore -> output -> closeTableBlock();	
						}
						
						// Open Block
						echo '<div class="fixed_height">
							<div class="left_float width_longer">
								<h2 class="block_title">' . htmlspecialchars( $forum_data['forum_name']) . '</h2>
							</div>
							<div class="right_float width_medium" style="margin-top: 12px; margin-right: 17px;">
								' . $appCore -> output -> toolsPopup( $forum_actions) . '
							</div>
							<div class="right_float width_small center" style="margin-top: 12px; padding-top: 4px; margin-right: 17px;">
								' . $this -> positionSelect( $forum_id, $forum_data['forum_pos'], $parent_stat[$forum_data['forum_parent']]) . '
							</div>
						</div>';
						echo $appCore -> output -> openTableBlock();	
						
						// We are in cat
						$in_cat = true;
					}
					else
					{
						echo $appCore -> output -> tableRow('<div class="cell left_float width_longer">
						' . $forum_data['item_image'] . $appCore -> output -> drawImage( 'forum_type_' . $forum_data['forum_type'], $appCore -> lang['acp_forum_form_type_' . $forum_data['forum_type']]) . ' <b> ' . htmlspecialchars( $forum_data['forum_name']) . '</b> <span class="item_description">' . sprintf( $appCore -> lang['acp_forums_list_stat'], $appCore -> number($forum_data['forum_posts']), $appCore -> number($forum_data['forum_threads'])) . '</span>
					</div>
					<div class="cell left_border right_float width_medium">
						' . $appCore -> output -> toolsPopup( $forum_actions) . '
					</div>
					<div class="cell left_border right_float width_small center" style="padding-top: 8px;">
						' . $this -> positionSelect( $forum_id, $forum_data['forum_pos'], $parent_stat[$forum_data['forum_parent']]) . '
					</div>');
					}
				}
				
				// Close Cat?
				if ( $in_cat)
				{
					echo $appCore -> output -> closeTableBlock();	
				}
				
				// Close stuff
				echo $appCore -> output -> openFormBlock();
				echo $appCore -> output -> submitButton( $appCore -> lang['acp_reoder_forums_form_submit']);
				echo $appCore -> output -> closeFormBlock();	
				echo $appCore -> output -> closeForm();
				
				// Draw output
				$appCore -> output -> draw( $appCore -> lang['acp_forums'], array( $appCore -> adminLink( 'act=forums') => $appCore -> lang['acp_forums']), ob_get_clean());
			}
		}
					
	}

	function positionSelect( $forum_id, $forum_pos, $forum_num){
		
		// Options
		$options = array();
		
		// Draw Options
		for ( $i = 0; $i < $forum_num; $i++)
		{
			$options[$i] = ( $i + 1);
		}
		
		// Return select
		return $this -> appCore -> drawList( 'forum_pos[' . $forum_id . ']', $options, $forum_pos, '70px');
	
	}
	
}